/***************************************************************************
 *
 * Copyright (c) 2012 Baidu.com, Inc. All Rights Reserved
 * common.h,v 1.0 2012-08-17 11:30 yelu01
 *
 **************************************************************************/



/**
 * @file file_map.h
 * @author yelu01(yelu01@baidu.com)
 * @date 2012-08-17 11:30
 * @version 1.0
 * @brief iterator interface implementation.
 *
 **/

#pragma once

#include "item.h"
#include <vector>

class iterator_t
{
public:
	virtual ~iterator_t(){}
	virtual int reset() = 0;
	virtual int get_next(pitem& item) = 0;
	virtual uint64_t size() = 0;
};

class vector_iterator_t:public iterator_t
{
public:
	vector_iterator_t(const std::vector<pitem>& vec):
	_vec(vec),
	_curr_index(0),
	_is_reset(false)
	{}
	int reset() { _curr_index = 0; _is_reset = true; return 0; }
	int get_next(pitem& item)
	{
		if(_curr_index >= _vec.size()) { return -1; }
		if(_is_reset)
		{
			item = _vec[_curr_index];
			_is_reset = false;
			return 0;
		}
		_curr_index++;
		if(_curr_index >= _vec.size()) { return -1; }
		item = _vec[_curr_index];
		return 0;
	}
	uint64_t size(){ return _vec.size(); }
private:
	const std::vector<pitem>& _vec;
	uint64_t _curr_index;
	bool _is_reset;
};

